﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KinectGame;

namespace MemoGame
{
    public partial class HallOfFameUI : KinectBaseForm
    {
        private int currentIndex;
        private IList<GameButton> gameButtons;

        public HallOfFameUI()
        {
            InitializeComponent();
            button1.BackgroundImage = new Bitmap(Image.FromFile("../../Resources/images/arrowleft.png"), 80, 80);
            button2.BackgroundImage = new Bitmap(Image.FromFile("../../Resources/images/arrowright.png"),80,80);

            currentIndex = Game.getInstance().GameSize;
            Console.WriteLine("CI "+currentIndex);
            initHOF(currentIndex);
            
        }

        public override void SetupForKinectMode()
        {
            base.SetupForKinectMode();
            this.gameButtons = new List<GameButton>();
            this.gameButtons.Add(button1);
            this.gameButtons.Add(btnMenu);
            this.gameButtons.Add(button2);
            this.button1.Click -= new System.EventHandler(this.button1_Click);
            this.btnMenu.Click -= new System.EventHandler(this.btnMenu_Click);
            this.button2.Click -= new System.EventHandler(this.button2_Click);
        }

        public override void highlightButton(int horizontalIndex, int verticalIndex)
        {
            if (horizontalIndex > 0) this.gameButtons[horizontalIndex - 1].Unhighlight();
            this.gameButtons[horizontalIndex].Highlight();
            if (horizontalIndex < this.gameButtons.Count - 1) this.gameButtons[horizontalIndex + 1].Unhighlight();
        }

        public override void Click(int horizontalIndex, int verticalIndex)
        {
            if (horizontalIndex == 0) Previous(); 
            if (horizontalIndex == 1) OpenMenu();
            if (horizontalIndex == 2) Next(); 
        }

        public override int SetHorizontalItemNumber()
        {
            return 3;
        }

        public override int SetVerticalItemNumber()
        {
            return 1;
        }

        private void btnMenu_Click(object sender, EventArgs e)
        {
            OpenMenu(); 
        }

        private void OpenMenu()
        {
            this.gameContext.ApplicationMode(this, new MenuUI(), 0);
        }

        private void initHOF(int index)
        {
            HallOfFame hof = Game.getInstance().HallOfFame;
            hof.Ranking.Clear();
            hof.fillRanking();
            hof.Ranking[index].Sort();

            for (int i = 1; i <= 10; i++)
            {
                if (hof.Ranking[index].Count < i)
                {
                    hof.Ranking[index].Add(new HOFItem("", ""));
                }
            }

            name1.Text = hof.Ranking[index].ElementAt(0).Player;
            name2.Text = hof.Ranking[index].ElementAt(1).Player;
            name3.Text = hof.Ranking[index].ElementAt(2).Player;
            name4.Text = hof.Ranking[index].ElementAt(3).Player;
            name5.Text = hof.Ranking[index].ElementAt(4).Player;
            name6.Text = hof.Ranking[index].ElementAt(5).Player;
            name7.Text = hof.Ranking[index].ElementAt(6).Player;
            name8.Text = hof.Ranking[index].ElementAt(7).Player;
            name9.Text = hof.Ranking[index].ElementAt(8).Player;
            name10.Text = hof.Ranking[index].ElementAt(9).Player;
            score1.Text = hof.Ranking[index].ElementAt(0).Time;
            score2.Text = hof.Ranking[index].ElementAt(1).Time;
            score3.Text = hof.Ranking[index].ElementAt(2).Time;
            score4.Text = hof.Ranking[index].ElementAt(3).Time;
            score5.Text = hof.Ranking[index].ElementAt(4).Time;
            score6.Text = hof.Ranking[index].ElementAt(5).Time;
            score7.Text = hof.Ranking[index].ElementAt(6).Time;
            score8.Text = hof.Ranking[index].ElementAt(7).Time;
            score9.Text = hof.Ranking[index].ElementAt(8).Time;
            score10.Text = hof.Ranking[index].ElementAt(9).Time;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Previous(); 
        }

        private void Previous()
        {
            Console.WriteLine("CI: " + currentIndex);
            if (currentIndex > 2)
            {
                currentIndex -= 2;
                initHOF(currentIndex);
                button2.Enabled = true;
                if (currentIndex == 2)
                {
                    button1.Enabled = false;
                }
                label13.Text = currentIndex.ToString();
                this.Refresh();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Next(); 
        }

        private void Next()
        {
            Console.WriteLine("CI: "+currentIndex);
            if (currentIndex < 8)
            {
                button1.Enabled = true;
                currentIndex += 2;
                initHOF(currentIndex);
                if (currentIndex == 8)
                {
                    button2.Enabled = false;
                }
                label13.Text = currentIndex.ToString();
                this.Refresh();
            }
        }
    }
}
